package org.zoomdev.zoom.aop.test.shop.modules;

import org.zoomdev.zoom.aop.AspectFactory;
import org.zoomdev.zoom.aop.Interceptor;
import org.zoomdev.zoom.aop.Invoker;
import org.zoomdev.zoom.aop.test.shop.interceptor.LoginNotifyInterceptor;
import org.zoomdev.zoom.aop.test.shop.services.UserModel;
import org.zoomdev.zoom.aop.test.shop.services.UserService;
import org.zoomdev.zoom.common.filter.MethodFilter;
import org.zoomdev.zoom.ioc.annotations.Module;
import org.zoomdev.zoom.ioc.annotations.ModuleInit;

import java.lang.reflect.Method;

@Module
public class UserConfigModule {
    @ModuleInit
    public void configInterceptor(AspectFactory factoryy, LoginNotifyInterceptor interceptor) {
        factoryy.createAspect(interceptor, new MethodFilter() {
            @Override
            public boolean accept(Class<?> clazz, Method method) {
                return (clazz == UserModel.class
                        && method.getName().equals("login")) ||
                        (clazz == UserService.class
                                && method.getName().equals("login"));
            }
        });
    }

    @ModuleInit
    public void config(AspectFactory factory) {

        factory.createAspect(new Interceptor() {
            @Override
            public Object intercept(Invoker invoker, Method method, Object target, Object[] args) throws Throwable {
                System.out.println("call" + method);
                return invoker.invoke(target, args);
            }
        }, new MethodFilter() {
            @Override
            public boolean accept(Class<?> clazz, Method method) {
                return clazz == UserService.class;
            }
        });
    }
}
